package com.xsg.printer.core;

import com.xsg.printer.core.conditions.ElementConfig;
import com.xsg.printer.core.conditions.TemplateStepConfig;
import com.xsg.printer.core.enums.AlignEnum;
import com.xsg.printer.core.enums.FloatAlignEnum;
import com.xsg.printer.core.enums.VoucherEnum;
import com.xsg.printer.core.model.XsgTemplate;

import javax.swing.*;
import java.awt.*;

/**
 * @author 高总辉
 * @desc
 * @date 2020/11/23 4:39 下午
 */
public class App {


    public static void main(String[] args) {
//        /*
//         * 在 AWT 的事件队列线程中创建窗口和组件, 确保线程安全,
//         * 即 组件创建、绘制、事件响应 需要处于同一线程。
//         */
//        EventQueue.invokeLater(new Runnable() {
//            @Override
//            public void run() {
//                // 创建窗口对象
//                MyFrame frame = new MyFrame();
//                // 显示窗口
//                frame.setVisible(true);
//            }
//        });
        draw();
    }

    /**
     * 窗口
     */
    public static class MyFrame extends JFrame {

        public static final String TITLE = "Java图形绘制";

        public static final int WIDTH = 1500;
        public static final int HEIGHT = 1000;

        public MyFrame() {
            super();
            initFrame();
        }

        private void initFrame() {
            // 设置 窗口标题 和 窗口大小
            setTitle(TITLE);
            setSize(WIDTH, HEIGHT);

            // 设置窗口关闭按钮的默认操作(点击关闭时退出进程)
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            // 把窗口位置设置到屏幕的中心
            setLocationRelativeTo(null);

            // 设置窗口的内容面板
            MyPanel panel = new MyPanel(this);
            setContentPane(panel);
        }

    }

    /**
     * 内容面板
     */
    public static class MyPanel extends JPanel {

        private MyFrame frame;

        public MyPanel(MyFrame frame) {
            super();
            this.frame = frame;
        }

        /**
         * 绘制面板的内容: 创建 JPanel 后会调用一次该方法绘制内容,
         * 之后如果数据改变需要重新绘制, 可调用 updateUI() 方法触发
         * 系统再次调用该方法绘制更新 JPanel 的内容。
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            String path = draw();
            if (path == null) {
                return;
            }
//            try {
//                Image image = ImageUtil.readImage(path, 800, 400, false);
//                g.drawImage(image, 200, 10, null);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }
    }

    public static String draw() {
        XsgTemplate template = TemplateStepConfig.builder().body(800, 800)
                .div(300, 200, FloatAlignEnum.RIGHT, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT, ElementConfig.builder()
                                        .qrCode("888888888", 50, 50)
                                        .build()
                                )
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                )
                .div(300, 200, FloatAlignEnum.NONE, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                )
                .div(300, 200, FloatAlignEnum.LEFT, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                )
                .div(300, 200, FloatAlignEnum.NONE, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                )
                .div(300, 200, FloatAlignEnum.RIGHT, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT)
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                )
                .div(300, 200, FloatAlignEnum.RIGHT, divConfig -> divConfig
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT, ElementConfig.builder()
                                        .text("wqwq",100,50)
                                        .build()
                                )
                                .div(100, 50, FloatAlignEnum.RIGHT)
                        )
                        .div(200, 100, FloatAlignEnum.RIGHT, divConfig1 -> divConfig1
                                .div(100, 50, FloatAlignEnum.RIGHT, ElementConfig.builder()
                                        .qrCode("89885", 50, 50, AlignEnum.TOP_RIGHT)
                                        .barCode("89885", 50, 50, AlignEnum.TOP_RIGHT)
                                        .build()
                                )
                                .div(100, 50, FloatAlignEnum.RIGHT, ElementConfig.builder()
                                        .text("wqwq", 100, 50)
                                        .build()
                                )
                        )
                )
                .build();
        return TemplateDrawHandler.getInstance().draw(template, VoucherEnum.IMAGE, null);
    }
}
